package com.factors.ZhangDingHou.Parenral_Involvement;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class ParentalInvolvementStatsReducer extends Reducer<Text, DoubleWritable, Text, DoubleWritable> {

    @Override
    protected void reduce(Text key, Iterable<DoubleWritable> values, Context context) throws IOException, InterruptedException {
        int count = 0;
        double totalScore = 0.0;

        for (DoubleWritable val : values) {
            totalScore += val.get();
            count++;
        }

        double averageScore = count > 0 ? totalScore / count : 0;

        context.write(key, new DoubleWritable(averageScore));
    }
}